package com.smartum.mpr.client.view;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartum.mpr.client.Authorization;
import com.smartum.mpr.client.event.WindowResizeHandler;
import com.smartum.mpr.client.presenter.HeaderPresenter;

/**
 * Header widget.
 *
 * @see HeaderPresenter
 */
public class HeaderView extends Composite implements HeaderPresenter.Display {
    private final Canvas userName;
    private final IButton logout;

    public HeaderView() {
        final Authorization authorizationBundle = com.google.gwt.core.shared.GWT.create(Authorization.class);

        final HLayout wrapper = new HLayout();
        wrapper.setAlign(Alignment.CENTER);
        wrapper.setWidth(Window.getClientWidth() + "px");
        wrapper.setStyleName("headerPanel");
        //TODO: try to move width to the css
        wrapper.setHeight("53px");

        final VLayout logoPanel = new VLayout();

        final HLayout nonLogoPanel = new HLayout();
        nonLogoPanel.setAlign(Alignment.RIGHT);
        nonLogoPanel.setWidth(Window.getClientWidth() - logoPanel.getOffsetWidth() + "px");

        final VLayout greetingPanel = new VLayout();
        greetingPanel.setWidth("10px");

        userName = new Canvas();
        userName.setHeight("10px");
        userName.setStyleName("greetingText");

        logout = new IButton(authorizationBundle.logout());

        greetingPanel.addMember(userName);
        greetingPanel.addMember(logout);

        nonLogoPanel.addMember(greetingPanel);

        wrapper.addMember(logoPanel);
        wrapper.addMember(nonLogoPanel);

        final WindowResizeHandler wrapperResizeHandler = new WindowResizeHandler(wrapper,
                53, 0, false);
        Window.addResizeHandler(wrapperResizeHandler);

        final WindowResizeHandler nonLogoResizeHandler = new WindowResizeHandler(nonLogoPanel, 53,
                logoPanel.getOffsetWidth(), false);
        Window.addResizeHandler(nonLogoResizeHandler);

        initWidget(wrapper);
    }

    public Canvas getUserName() {
        return userName;
    }

    public IButton getLogout() {
        return logout;
    }

    public Widget getHeaderWidget() {
        return this;
    }
}
